package com.htstar.ovms.enterprise.handle.listener;


import com.alibaba.excel.ExcelWriter;
import com.htstar.ovms.common.core.constant.SecurityConstants;
import com.htstar.ovms.common.excel.util.ExcelHandler;
import com.htstar.ovms.device.api.constant.ExportRecordConstant;
import com.htstar.ovms.device.api.entity.ExportRecord;
import com.htstar.ovms.device.api.feign.ExportRecoreFeign;
import com.htstar.ovms.enterprise.api.req.ExportReq;
import com.htstar.ovms.enterprise.api.vo.CarMotItemPageVO;
import com.htstar.ovms.enterprise.api.vo.CarRepairItemPageVO;
import com.htstar.ovms.enterprise.handle.event.CarMotItemExcelExportEvent;
import com.htstar.ovms.enterprise.handle.event.CarRepairItemExcelExportEvent;
import com.htstar.ovms.enterprise.service.CarMotItemService;
import com.htstar.ovms.enterprise.service.CarRepairItemService;
import com.htstar.ovms.enterprise.utils.ExprotUploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class CarRepairItemExcelExportListener implements ApplicationListener<CarRepairItemExcelExportEvent> {
    @Autowired
    private CarRepairItemService carRepairItemService;
    @Autowired
    private ExprotUploadUtil exprotUploadUtil;
    @Autowired
    private  ExportRecoreFeign exportRecoreFeign;
    @Value("${excel.queryLimit}")
    private int queryLimit;
    /**
     * jinzhu  后台导出维修记录监听器
     * @return
     */
    @Override
    @Async
    public void onApplicationEvent(CarRepairItemExcelExportEvent carRepairItemExcelExportEvent) {
        ExportReq carSecurityAlarmDTO = carRepairItemExcelExportEvent.getExportReq();
        ExportRecord exportRecord = new ExportRecord();
        exportRecord.setId(carSecurityAlarmDTO.getExportId());//更新导出表的实体
        List<CarRepairItemPageVO> carInsItemPageVOS = carRepairItemService.exportExcel(carSecurityAlarmDTO);
        int numSheet = 1;
        int sheetLimit = 500;
        if (carInsItemPageVOS.size() > sheetLimit) {
            numSheet = carInsItemPageVOS.size() / queryLimit + 1;
        }
        int alarmCountPage = 1; //分页查询：每次5000
        if (carInsItemPageVOS.size() > queryLimit) {
            alarmCountPage = (carInsItemPageVOS.size() / queryLimit) + 1;
        }

        int count = 0;
        int num = 0;
        ExcelHandler handler = null;
        ExcelWriter excelWriter = null;
        try {
            //创建handler对象--参数:文件夹名
            handler = new ExcelHandler("CarRepairItemExcel/" + carSecurityAlarmDTO.getUserId());
            excelWriter = handler.create(carSecurityAlarmDTO.getFileName(), CarRepairItemPageVO.class, numSheet);

            List<CarRepairItemPageVO> list = new ArrayList<>(1024);
            for (int t = 1; t <= alarmCountPage; t++) {//模拟分页页数 每页50W数据
                carSecurityAlarmDTO.setCurrent(t);
                carSecurityAlarmDTO.setSize(queryLimit);
                list = carInsItemPageVOS;
                //count 将控制插入哪一个sheet
                count += list.size();
                handler.write(excelWriter, list, count);
                list.clear();//必须clear,否则数据会重复
            }
            //上传文件
        } catch (Exception ex) {
            exportRecord.setExportStatus(ExportRecordConstant.EXPORT_FAILED);
            exportRecoreFeign.updateById(exportRecord, SecurityConstants.FROM_IN);
            ex.printStackTrace();
        } finally {
            if (null != excelWriter) {
                handler.finish(excelWriter);
            }
            String fileNames="CarRepairItemExcel/" + carSecurityAlarmDTO.getUserId()+"/"+carSecurityAlarmDTO.getFileName();
            exprotUploadUtil.uploadExcel(carSecurityAlarmDTO.getFileName(),fileNames,"excel",exportRecord);
        }
    }

//    @EventListener
//    @Async
//    public void onApplicationEvent(AlarmExcelExportEvent alarmExcelExportEvent) {
//        CarSecurityAlarmDTO carSecurityAlarmDTO = alarmExcelExportEvent.getCarSecurityAlarmDTO();
//        Integer alarmCount = deviceAlarmService.getAlarmByTypeAndTimeListCount(carSecurityAlarmDTO);
//
//        int numSheet = 1;
//        if (alarmCount > 1000000) {
//            numSheet = alarmCount / 1000000 + 1;
//        }
//        int alarmCountPage = 1; //分页查询：每次5000
//        if (alarmCount > 5000) {
//            alarmCountPage = (alarmCount / 5000) + 1;
//        }
//
//        int count = 0;
//        int num = 0;
//        ExcelHandler handler = null;
//        ExcelWriter excelWriter = null;
//        try {
//            //创建handler对象--参数:文件夹名
//            handler = new ExcelHandler("test01");
//            excelWriter = handler.create("记录", DeviceAlarmExcelVO.class, 10);
//
//            List<DeviceAlarmExcelVO> list = new ArrayList<>(1024);
//            for (int t = 1; t <= alarmCountPage; t++) {//模拟分页页数 每页50W数据
//
//                carSecurityAlarmDTO.setCurrent(t);
//                carSecurityAlarmDTO.setSize(5000);
//                list = deviceAlarmService.getAlarmByTypeAndTimeListForExcel(carSecurityAlarmDTO);
//                //count 将控制插入哪一个sheet
//                count += list.size();
//                handler.write(excelWriter, list, count);
//                list.clear();//必须clear,否则数据会重复
//            }
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        } finally {
//            if (null != excelWriter) {
//                handler.finish(excelWriter);
//            }
//        }
//    }
}
